package com.mantru.iotpot.Cook.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.mantru.iotpot.R;

import java.util.List;

public class NunberKeyAdapter extends BaseAdapter {
    private Context context;
    private List<String> items;
    private OnNumberClickListener listener; // 定义点击事件接口

    public interface OnNumberClickListener {
        void onNumberClick(String number);
    }
    public NunberKeyAdapter(Context context, List<String> items,OnNumberClickListener listener) {
        this.context = context;
        this.items = items;
        this.listener = listener;
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        String number = items.get(position);
        LayoutInflater inflater = LayoutInflater.from(context);
        // 判断是0还是删除按钮，使用不同的布局
        if (number.equals("")) { // 使用slider来表示 delete
            // 如果是0或删除按钮，使用number_key_delete_button布局
            if (view == null) { // 标记view类型
                view = inflater.inflate(R.layout.number_key_delete_button, parent, false);
            }
            // 分别设置0和删除按钮的点击事件
            Button button0 = view.findViewById(R.id.number_key_0);
            Button buttonDelete = view.findViewById(R.id.number_key_delete);
            button0.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onNumberClick("0");
                    Log.d("numberkey", "adapter_number: 0");
                }
            });
            buttonDelete.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onNumberClick("删除");
                    Log.d("numberkey", "adapter_number: 删除");
                }
            });
        } else {
            // 其他数字按钮使用普通布局
            if (view == null) { // 如果当前view是delete类但需要普通布局
                view = inflater.inflate(R.layout.number_key_button, parent, false);
            }
            Button button = view.findViewById(R.id.number_key);
            button.setText(number);
            button.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onNumberClick(number);
                    Log.d("numberkey", "adapter_number: " + number);
                }
            });
        }
        return view;
    }
}
